﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Interfaces;
using System.IO;
namespace Findr
{
    public delegate int NewMarkerDelegate(double latitude, double longitude, System.Drawing.Bitmap bitmap, string text);
    public delegate void MoveMarkerDelegate(int id, double latitude, double longitude);

    public class Findr : IApplication
    {
        Communicator comm = new Communicator();
        System.Windows.Forms.Timer UpdateTimer = new System.Windows.Forms.Timer();
        
        //Declarations of all our internal plugin variables
        IMiddleware host = null;
        ctlMain mainInterface;
        Contacts ContactList;
        System.Drawing.Bitmap bmpNobody;

        public int OfflineProtoId;

        public Findr()
		{
            ContactList = new Contacts(ref comm);
            mainInterface = new ctlMain(ref comm);
		}

        public int NewMarkerHnd(double latitude, double longitude, System.Drawing.Bitmap bitmap, string text)
        {
            if (bitmap == null && bmpNobody != null)
            {
                return Host.NewMarker(latitude, longitude, OfflineProtoId, text, bmpNobody);
            }
            else
            {
                return Host.NewMarker(latitude, longitude, OfflineProtoId, null, bitmap);
            }
        }

        public void MoveMarkerHnd(int id, double latitude, double longitude)
        {
            Host.MoveMarker(id, latitude, longitude);
        }

        void UpdateTimer_Tick(object sender, EventArgs e)
        {
            comm.GetContactList();
            Host.WriteDebugLog("Contact list update...");
        }

        /// <summary>
        /// Name of the plugin.
        /// </summary>
        public string Name
        {
            get { return "Findr"; }
        }

		/// <summary>
		/// Description of the Plugin's purpose
		/// </summary>
		public string Description
		{
            get { return "Buddy finder"; }
		}

		/// <summary>
		/// Author of the plugin
		/// </summary>
		public string Author
		{
            get { return "pazsoo"; }
        }

        public string Version
        {
            get { return "0.1"; }
        }

		/// <summary>
		/// Host of the plugin.
		/// </summary>
		public IMiddleware Host
		{
            get { return host; }
            set { host = value; }
		}

		public System.Windows.Forms.UserControl MainInterface
		{
            get { return mainInterface; }
		}

        public void Initialize()
        {
            comm.ApplicationPath = Host.WorkingDirectory;
            Contacts.ApplicationPath = Host.WorkingDirectory;

            if (!Directory.Exists(Host.WorkingDirectory + "\\plugins\\Findr\\avatars"))
            {
                Directory.CreateDirectory(Host.WorkingDirectory + "\\plugins\\Findr\\avatars");
            }

            bmpNobody = new System.Drawing.Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Findr.Resources.nobody.jpg"));
            System.IO.Stream bgImage = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Findr.Resources.marker1.png");

            OfflineProtoId = host.CreateTemplate(bgImage, new System.Drawing.Point(0, -10), new System.Drawing.Point(0, -77), this.GetHashCode().ToString());

            Contacts.NewMarkerHandler = new NewMarkerDelegate(NewMarkerHnd);
            Contacts.MoveMarkerHandler = new MoveMarkerDelegate(MoveMarkerHnd);

            UpdateTimer.Tick += new EventHandler(UpdateTimer_Tick);
            UpdateTimer.Interval = Settings.UpdatePeriod;
            UpdateTimer.Enabled = true;
        }

        public void Dispose()
        {

        }

        CustomControls.AlphaImage IModule.IconEnabled
        {
            get { return CustomControls.AlphaImage.CreateFromResource("Findr.Resources.icon_enabled.png"); }
        }

        CustomControls.AlphaImage IModule.IconDisabled
        {
            get { return CustomControls.AlphaImage.CreateFromResource("Findr.Resources.icon_disabled.png"); }
        }

        public bool Sleep()
        {
            throw new NotImplementedException();
        }

        public bool Wake()
        {
            throw new NotImplementedException();
        }
    }
}
